<template>
    <LModalForm 
        :visible="visible"
        :confirmLoading="confirmLoading" 
        :title="title" 
        ref="attribute" 
        :formAttributes="formAttributes" 
        @handleOk="handleOk" 
        @handleCancel="handleCancel"
        @clickEventHandler="clickEventHandler"
        @changeEventHandler="changeEventHandler"></LModalForm>

</template>

<script>
import LModalForm from "@/components/lt/form/LModalForm"
import pageSetting from '@/components/lt/codeGenerator/pageSetting'
import pick from 'lodash.pick'
import defaultSettings from '@/config/defaultSettings'

export default {
    name:'QueryFormAttribute',
    components:{
        LModalForm
    },
    data(){
        return{
            title:'详情',
            visible:false,
            confirmLoading: false,
            model:{},
            formAttributes:{
              type:'',//collapse,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'label',label:'标题',component:pageSetting.常量.表单组件类型.单行文本,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'field',label:'字段代码',component:pageSetting.常量.表单组件类型.单行文本,disabled:true,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'entityClassName',label:'实体名称',component:pageSetting.常量.表单组件类型.单行文本,disabled:true},
                    {field:'dbField',label:'数据库字段',component:pageSetting.常量.表单组件类型.单行文本,disabled:true},
                    {field:'component',label:'组件',component:pageSetting.常量.表单组件类型.下拉选择,options:pageSetting.数据字典.查询表单组件类型,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'condition',label:'条件',component:pageSetting.常量.表单组件类型.下拉选择,options:pageSetting.数据字典.查询表单查询条件,rules: [{ required: true, message: '不能为空!' }],initialValue:'eq'},
                    {field:'required',label:'是否必填',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:false,rules: [{ required: false, message: '不能为空!' }]},
                    {field:'disabled',label:'是否禁用',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:false,rules: [{ required: false, message: '不能为空!' }]},
                   
                    //下拉和树的
                    {field:'dictCode',label:'数据字典',component:pageSetting.常量.表单组件类型.下拉选择,dictCode:defaultSettings.数据字典.数据字典名称,rules: [{ required: true, message: '不能为空!' }]},
                   //下拉的
                    {field:'parentSelect',label:'父级下拉(级联用)',component:pageSetting.常量.表单组件类型.单行文本},
                    {field:'mode',label:'多选模式',component:pageSetting.常量.表单组件类型.下拉选择,options:pageSetting.数据字典.下拉多选模式},

                    //下拉树的
                    {field:'multiple',label:'是否多选',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:false,rules: [{ required: false, message: '不能为空!' }],},
                    {field:'treeCheckable',label:'是否显示复选框',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:false,rules: [{ required: false, message: '不能为空!' }],},

                 ]
                },
              ]

            }


        }
    },
    methods:{
       
        edit (record) {
            this.visible = true
            this.model=record
            this.changeComponent(this.model)
            this.$nextTick(() => {
                this.$refs.attribute.getForm().setFieldsValue(pick(this.model,'label','field','disabled','condition','required','component','dictCode','parentSelect','mode','multiple','treeCheckable','entityClassName','dbField'))
            })
            
        },
        handleOk(){
            this.$refs.attribute.getForm().validateFields((err, values) => {
                if (!err) {
                    this.model = Object.assign(this.model, values)
                    this.setRules()
                    this.close()
                }
            })
            
        },
        handleCancel(){
            this.visible=false
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
        changeComponent(item){
            if(item["component"]==pageSetting.常量.表单组件类型.下拉选择){
                this.formAttributes.formItems[0].fields[8]['rules'][0]["required"]=true
      
         
            }else if(item["component"]==pageSetting.常量.表单组件类型.树选择){
              
                this.formAttributes.formItems[0].fields[8]['rules'][0]["required"]=true

                    
            }else{
                this.formAttributes.formItems[0].fields[8]['rules'][0]["required"]=false
                  
            }
           
            
        },
        changeEventHandler(changeItem){
            if(changeItem["component"]){
                this.changeComponent(changeItem)
               
            }
            
        },
        clickEventHandler(field,form){
            
        },
        setRules(){
            this.model["rules"] = JSON.stringify([{ required: this.model["required"], message: '不能为空!' }])
        },
      
    },
   
}
</script>